import pyqrcode
import tilemap
from PIL import Image

URL = "http://www.discoveranywheremobile.com/"
#URL = "http://m.visitpalmsprings.com/listings/x-1fe89fc957e563c7/x-08cdb36014211c43/l-22c2cdd72d95ff48/"

qr_image = pyqrcode.MakeQRImage(URL, rounding = 0, fg = "black", bg = "white", br = False)
print qr_image
qr_image.save("file.png","PNG")

def genQRCode(URL, fg = None, d = 0.4):
        qr_image = pyqrcode.MakeQRImage(URL, rounding = 0, fg = "black", bg = "white", br = False)
        if (fg == None):
                return qr_image
        fg_image = Image.open(fg)
        width, height = qr_image.size
        print width,height
        fg_image = fg_image.resize((width,height))
        qr_data = list(qr_image.getdata())
        fg_data = list(fg_image.getdata())
        for pix in range(0,len(qr_data)):
                if qr_data[pix] == (0,0,0,255):
                        qr_data[pix] = tuple([int(d*p+(1-d)*0) for p in fg_data[pix]])
                else:
                        qr_data[pix] = tuple([int(d*p+(1-d)*255) for p in fg_data[pix]])
        qr_image.putdata(qr_data)
        return qr_image

#qr = genQRCode(URL, "jamnik.jpg")
#qr = genQRCode(URL, "chrome-logo.png")
#qr.save("qr.png", "PNG")
#qr.show()

def demo():
        qr_image = pyqrcode.MakeQRImage(URL, rounding = 0, fg = "black", bg = "white", br = False)
        qr_data = [pix == (0,0,0,255) for pix in list(qr_image.getdata())]
        dim = qr_image.size
        bwmap = dict(((x,y),qr_data[y*dim[0]+x]) for x in range(0,dim[0])
                        for y in range(0,dim[1]))
        tm = tilemap.TileMap(bwmap, dim)
        for x in range(0,dim[0]):
                for y in range(0,dim[1]):
                        if tm.is_taken((x,y)):
                                print "wut"

demo()
